//
//  main.c
//  108. sortedArrayToBST
//
//  Created by edz on 2020/7/13.
//  Copyright © 2020 edz. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>

 // Definition for a binary tree node.
 struct TreeNode {
     int val;
     struct TreeNode *left;
     struct TreeNode *right;
 };
 

struct TreeNode* sortedArrayToBST(int* nums, int numsSize){
    int iRoot = 0;
    struct TreeNode* pCurNode = NULL;

    //1,结束条件
    if((NULL == nums) || (0 == numsSize)) return NULL;

    //2,初始化
    pCurNode = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    //3,定位根节点
    iRoot = numsSize / 2;

    //4,递归处理左右支
    pCurNode->val = nums[iRoot];
    pCurNode->left = sortedArrayToBST(&nums[0], iRoot);
    pCurNode->right = sortedArrayToBST(&nums[iRoot + 1], numsSize - iRoot - 1);
    return pCurNode;
}
